package cn.wolfcode.luowowo.website.controller;


import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateSearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotion.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference //缓存服务中的攻略统计数量   阅读数操作
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private IStrategyCommendService iStrategyCommendService;

//
    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;

    @RequestMapping("detail")
    public String detail(Model model, Long id) {
        //detail
        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContent(id);
        detail.setStrategyContent(content);
        model.addAttribute("detail", detail);

//        阅读量 + 1
        strategyStatisRedisService.viewNumIncrease(id, 1);//根据id判断vo对象是否存在,+1;

        //热门排序分数 + 1  : 浏览数+评论数
        strategyStatisRedisService.addHotScores(id, 1);

        //获取到key对应的value的值,共享数据
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(id);
        model.addAttribute("vo", vo);
        return "strategy/detail";
    }

    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {
        //toast
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        //dest
        model.addAttribute("dest", dest);
        //tags

        List<StrategyTag> list = strategyTagService.list();
        model.addAttribute("tags", list);
        //pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "strategy/list";
    }

    //攻略评论添加
    @RequestMapping("commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo) {
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setCity(userInfo.getCity());
        comment.setLevel(userInfo.getLevel());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        strategyCommentService.saveOrUpdate(comment);

        //评论数的+1操作
        //increaseReplyNum 参数一:表示通过攻略ID +1
        strategyStatisRedisService.increaseReplyNum(comment.getDetailId(), 1);

        //通过攻略id获取对应的value的对象
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(comment.getDetailId());
        //暂时使用1,后续进行数据存在再怎么说处理

        //热门排序分数 + 1  : 浏览数+评论数
        strategyStatisRedisService.addHotScores(comment.getDetailId(), 1);

        return AjaxResult.SUCCESS.addData(vo.getReplynum());
    }

    //评论的列表
    @RequestMapping("/comment")
    public String commentPage(Model model, @ModelAttribute("qo") StrategyCommentQuery qo) {
        model.addAttribute("page", strategyCommentService.query(qo));
        return "strategy/commentTpl";
    }

    //评论的点赞
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, Long fromid) {
        //点赞  与 取消点赞
        strategyCommentService.thumbUp(toid, fromid);

        return AjaxResult.SUCCESS;
    }

    //攻略收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {  //登录处理
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);  //102 事先约定好未登录状态码
            return result;
        }
        //收藏与取消收藏, 根据返回true/false决定
        boolean ret = strategyStatisRedisService.favor(sid, userInfo.getId());
        StrategyStatisVO vo = strategyStatisRedisService.getStrategyStatisVo(sid);

        //国外排序排行加分
        strategyStatisRedisService.addScoreAbroadAndChina(sid, ret ? 1 : -1);
        return new AjaxResult(ret, "").addData(vo);
    }

    //攻略点赞
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {  //登录处理
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);  //102 事先约定好未登录状态码
            return result;
        }
        //点赞,置顶操作
        boolean ret = strategyStatisRedisService.thumbup(sid, userInfo.getId());

        //点赞国外排序加分
        if (ret) {
            strategyStatisRedisService.addScoreAbroadAndChina(sid, 1);
        }
        return new AjaxResult(ret, "").addData(strategyStatisRedisService.getStrategyStatisVo(sid));
    }
    //热门排序添加
    @RequestMapping("")
    public String index(Model model){
        //攻略推荐,获取commends
        //1:获取页面排序数据
        List<StrategyCommend> commends = iStrategyCommendService.getTop5();
        model.addAttribute("commends", commends);
        //2: 热门排序
        List<StrategyStatisVO> hotCds=strategyStatisRedisService.queryHotSortTop10();
        model.addAttribute("hotCds",hotCds);
        //: 国外排序
        List<StrategyStatisVO> abroadCds =strategyStatisRedisService.queryAbroadSortTop10();
        model.addAttribute("abroadCds",abroadCds);
        //2: 国内排序
        List<StrategyStatisVO> unabroadCds =strategyStatisRedisService.queryChinaSortTop10();
        model.addAttribute("unabroadCds",unabroadCds);


        //主题推荐: List<Map<String, Object>>
        //        //外层: list集合
        //内层: 左边:主题(id  name)  右边: 目的地集合  List集合中目的地:  (id name)
        //目前存在问题:
        //1:排序, 按主题关联到攻略的个数
        //2:只需要前面10个主题
        //3:目的地集合中数据去除重复
        List<Map<String, Object>> data = strategyTemplateSearchService.getThemeCommend();//方法获取主题推荐
        model.addAttribute("themeCds", data);


        //国内条件: 通过省份id 跟省份名 分  : list<StatisVO>  vo(id, name, count)
        List<StatisVO> chinas = strategyTemplateSearchService.queryConditionChinas();
        model.addAttribute("chinas", chinas);
        //国外条件: 通过国家id 国家的名 分
        List<StatisVO> abroads = strategyTemplateSearchService.queryConditionAbroad();
        model.addAttribute("abroads", abroads);

        //主题条件: 通过主题id 主题的名 分
        List<StatisVO> themes = strategyTemplateSearchService.queryConditionTheme();
        model.addAttribute("themes", themes);


        return "strategy/index";
    }

    //首页条件查询
    @RequestMapping("/searchPage")
    public  String searchPage(Model model, @ModelAttribute("qo")SearchQueryObject qo){
        //page
        Page page = strategyTemplateSearchService.query(qo);
        model.addAttribute("page", page);
        return "strategy/searchPageTpl";
    }
}

